﻿package mortal.game.view.copy.defenseCopy
{
    import Message.Public.*;
    import com.gengine.resource.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.defenseCopy.view.*;
    import mortal.game.view.mainUI.Navbar.*;
    import mortal.mvc.core.*;

    public class DefenseCopyNavBar extends View
    {
        private var _sp:Sprite;
        private var _equip1:DefenseEquipItem;
        private var _equip2:DefenseEquipItem;
        private var _equip3:DefenseEquipItem;
        private var _equip4:DefenseEquipItem;
        private var _equip5:DefenseEquipItem;
        private var _equip6:DefenseEquipItem;
        private var _btnBag:GLoadingButton;
        private var _btnShop:GLoadingButton;
        private var _btnChange:GLoadingButton;
        private var _uiInitOver:Boolean = false;
        private var _tweenHeight:int = 75;
        private var _isTweenCom:Boolean = true;

        public function DefenseCopyNavBar()
        {
            this.layer = LayerManager.uiLayer;
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.onShortcutBarResize();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.defenseCopy, this.showSkin);
            return;
        }// end function

        private function showSkin() : void
        {
            if (isDisposed)
            {
                return;
            }
            this.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._sp = UIFactory.sprite(0, 0, this);
            this._sp.y = this._tweenHeight;
            this._equip1 = UIFactory.getUICompoment(DefenseEquipItem, 65, 16, this._sp, EEquip._EEquipWeapon);
            this._equip2 = UIFactory.getUICompoment(DefenseEquipItem, 65 + 44 * 1, 16, this._sp, EEquip._EEquipNecklace);
            this._equip3 = UIFactory.getUICompoment(DefenseEquipItem, 65 + 44 * 2, 16, this._sp, EEquip._EEquipHelmet);
            this._equip4 = UIFactory.getUICompoment(DefenseEquipItem, 65 + 44 * 3, 16, this._sp, EEquip._EEquipClothes);
            this._equip5 = UIFactory.getUICompoment(DefenseEquipItem, 65 + 44 * 4, 16, this._sp, EEquip._EEquipGlove);
            this._equip6 = UIFactory.getUICompoment(DefenseEquipItem, 65 + 44 * 5, 16, this._sp, EEquip._EEquipShoe);
            this._equip1.doubleClickEnabled = true;
            this._equip2.doubleClickEnabled = true;
            this._equip3.doubleClickEnabled = true;
            this._equip4.doubleClickEnabled = true;
            this._equip5.doubleClickEnabled = true;
            this._equip6.doubleClickEnabled = true;
            this._btnBag = UIFactory.gLoadingButton(ResFileConst.DefenseCopyPackBtn, 339, 28, 26, 27, this._sp);
            this._btnBag.configEventListener(MouseEvent.CLICK, this.onBtnBagClickHandler);
            this._btnBag.name = "defenseCopyBagBtn";
            this._btnShop = UIFactory.gLoadingButton(ResFileConst.DefenseCopyShopBtn, 368, 28, 26, 27, this._sp);
            this._btnShop.configEventListener(MouseEvent.CLICK, this.onBtnShopClickHandler);
            this._btnShop.name = "defenseCopyShopBtn";
            this._btnChange = UIFactory.gLoadingButton(ResFileConst.ChangeBtn, 460, 28, 31, 25, this);
            this.configEventListener(DragEvent.Event_Move_In, this.itemMoveInHandler);
            this.configEventListener(DragEvent.Event_Throw_goods, this.itemThrowHandler);
            this.configEventListener(MouseEvent.DOUBLE_CLICK, this.onDoubleClickHandler);
            Dispatcher.addEventListener(EventName.ShortcutBarResize, this.onShortcutBarResize);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemAdd, this.onAddItem);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyRoleEquipAdd, this.onRoleEquipAdd);
            this.TweenFunc();
            this._uiInitOver = true;
            this.updateEquip();
            return;
        }// end function

        public function updateEquip() : void
        {
            var _loc_3:DefenseEquipItem = null;
            var _loc_4:ItemData = null;
            if (this._uiInitOver == false)
            {
                return;
            }
            var _loc_1:* = Cache.instance.pack.defenseRoleEquipCache.allItems;
            var _loc_2:* = _loc_1.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_2)
            {
                
                _loc_3 = this["_equip" + (_loc_5 + 1)] as DefenseEquipItem;
                _loc_4 = _loc_1[_loc_5] as ItemData;
                if (_loc_4)
                {
                    _loc_3.itemData = _loc_4;
                }
                else
                {
                    _loc_3.itemData = null;
                }
                _loc_5++;
            }
            return;
        }// end function

        private function onRoleEquipAdd(param1:Object) : void
        {
            var _loc_3:DefenseEquipItem = null;
            var _loc_2:* = param1 as ItemData;
            var _loc_4:int = 1;
            while (_loc_4 <= 6)
            {
                
                _loc_3 = this["_equip" + _loc_4] as DefenseEquipItem;
                if (_loc_3.itemData && _loc_3.itemData.serverData.uid == _loc_2.serverData.uid)
                {
                    _loc_3.addEffect();
                    break;
                }
                _loc_4++;
            }
            return;
        }// end function

        private function onAddItem(param1:Object) : void
        {
            var _loc_2:* = param1 as ItemData;
            if (_loc_2.serverData.posType == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyBag)
            {
                this.updateBagNotice(true);
            }
            return;
        }// end function

        public function updateBagNotice(param1:Boolean) : void
        {
            if (param1)
            {
                EffectManager.glowFilterReg(this._btnBag, [FilterConst.chatTipsFilter], 0.6, 20, 2, 0);
            }
            else
            {
                EffectManager.glowFilterUnReg(this._btnBag);
            }
            return;
        }// end function

        private function findItemDataFromCache(param1:DefenseEquipItem) : ItemData
        {
            var _loc_3:ItemData = null;
            var _loc_2:* = Cache.instance.pack.defenseRoleEquipCache.allItems;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.length)
            {
                
                _loc_3 = _loc_2[_loc_4] as ItemData;
                if (_loc_3)
                {
                    if (this.equipMapType(param1) == _loc_3.itemInfo.type)
                    {
                        return _loc_3;
                    }
                }
                else
                {
                    return null;
                }
                _loc_4++;
            }
            return null;
        }// end function

        private function onDoubleClickHandler(event:MouseEvent) : void
        {
            var _loc_3:Object = null;
            var _loc_2:* = event.target as DefenseEquipItem;
            if (_loc_2 && _loc_2.itemData != null)
            {
                _loc_3 = {};
                _loc_3.putOnUid = "";
                _loc_3.putOffUid = _loc_2.itemData.uid;
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyTakeEquip, _loc_3));
            }
            return;
        }// end function

        private function onBtnBagClickHandler(event:MouseEvent) : void
        {
            DefenseCopyBag.instance.popUp();
            this.updateBagNotice(false);
            return;
        }// end function

        private function onBtnShopClickHandler(event:MouseEvent) : void
        {
            LayerManager.windowLayer.tweenCenterWindow(DefenseCopyShopNew.instance, DefenseCopyBag.instance);
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            switch(event.target)
            {
                case this._btnChange:
                {
                    this.TweenFunc();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function itemMoveInHandler(event:DragEvent) : void
        {
            var _loc_3:Object = null;
            var _loc_2:* = event.dragSouce as ItemData;
            if (_loc_2.serverData.posType == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyBag && ItemsUtil.isDefenseEquip(_loc_2))
            {
                _loc_3 = {};
                _loc_3.putOnUid = _loc_2.uid;
                _loc_3.putOffUid = "";
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyTakeEquip, _loc_3));
            }
            return;
        }// end function

        private function itemThrowHandler(event:DragEvent) : void
        {
            var _loc_2:* = event.dragItem as DefenseEquipItem;
            Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyThrowEquip, _loc_2.itemData.uid));
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this._uiInitOver == false)
            {
                return;
            }
            this._uiInitOver = false;
            this._btnBag.name = "";
            this._btnShop.name = "";
            Dispatcher.removeEventListener(EventName.ShortcutBarResize, this.onShortcutBarResize);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemAdd, this.onAddItem);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyRoleEquipAdd, this.onRoleEquipAdd);
            NavbarModule(GameController.navbar.view).TweenFunc("up");
            this._isTweenCom = true;
            DisplayUtil.removeMe(this._sp);
            this._sp = null;
            this._equip1.dispose(param1);
            this._equip2.dispose(param1);
            this._equip3.dispose(param1);
            this._equip4.dispose(param1);
            this._equip5.dispose(param1);
            this._equip6.dispose(param1);
            this._btnBag.dispose(param1);
            this._btnShop.dispose(param1);
            this._btnChange.dispose(param1);
            this._btnBag = null;
            this._btnChange = null;
            return;
        }// end function

        public function TweenFunc() : void
        {
            var _loc_1:String = null;
            if (this._isTweenCom == false)
            {
                return;
            }
            if (this._tweenHeight == 75)
            {
                _loc_1 = "up";
            }
            if (this._tweenHeight == 0)
            {
                _loc_1 = "down";
            }
            if (_loc_1 == "up")
            {
                this._isTweenCom = false;
                this._tweenHeight = 0;
                TweenMax.to(this._sp, 0.4, {delay:0.4, y:this._tweenHeight, onComplete:this.myFunction});
                NavbarModule(GameController.navbar.view).TweenFunc("down");
            }
            else if (_loc_1 == "down")
            {
                this._isTweenCom = false;
                this._tweenHeight = 75;
                TweenMax.to(this._sp, 0.4, {y:this._tweenHeight, onComplete:this.myFunction});
                NavbarModule(GameController.navbar.view).TweenFunc("up");
            }
            return;
        }// end function

        private function myFunction() : void
        {
            this._isTweenCom = true;
            return;
        }// end function

        private function onShortcutBarResize(event:DataEvent = null) : void
        {
            this.x = GameController.shortcut.view.x + 390;
            this.y = SceneRange.display.height - 75;
            return;
        }// end function

        public function equipMapType(param1:DefenseEquipItem) : int
        {
            switch(param1)
            {
                default:
                {
                    break;
                }
            }
            return 0;
        }// end function

        public function getBtnBagGlobalPoint() : Point
        {
            if (this._btnBag == null || this._btnBag.parent == null)
            {
                return new Point();
            }
            var _loc_1:* = new Point(this._btnBag.x, this._btnBag.y);
            return this._btnBag.parent.localToGlobal(_loc_1);
        }// end function

        public function getEquipItemPointByUid(param1:String) : Point
        {
            var _loc_4:DefenseEquipItem = null;
            var _loc_2:* = Cache.instance.pack.defenseRoleEquipCache.allItems;
            var _loc_3:* = _loc_2.length - 1;
            var _loc_5:int = 0;
            while (_loc_5 <= _loc_3)
            {
                
                if (_loc_2[_loc_5] && ItemData(_loc_2[_loc_5]).uid == param1)
                {
                    _loc_4 = this["_equip" + (_loc_5 + 1)] as DefenseEquipItem;
                    return _loc_4.parent.localToGlobal(new Point(_loc_4.x, _loc_4.y));
                }
                _loc_5++;
            }
            return null;
        }// end function

    }
}
